<?php
    class controller_sentmail extends __registry_controller
    {
        public $_ = array();
        public $_layout = NULL;
        public $_content_header = 'OPEN';
        public $_content_top = 'OPEN';

        public function __construct()
        {
            parent::add_object(array(
                'handle_string' ,
                'handle_upload' ,
                'handle_paging' ,
                'model_sentmail' ,
                'handle_validate' ,
                "handle_permission",
                'model_users' ,
                'model_categories' ,
                'model_language'
            ));
            $this->_layout = NULL;

        }

        public function index_action()
        {
            $this->per_check_permission();
            $limit = 15;
            $res_html = NULL;
            $seo2 = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
            $enseo = end($this->_seokey);
            if (empty($seo2) || ($seo2 == $enseo && intval($enseo) != 0)) {
                $this->_['url_add'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('add') . '/';
                $this->_['url_edit'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('edit') . '/';
            } else {
                $this->_['url_add'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('add') . '/' . $seo2 . '/';
                $this->_['url_edit'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('edit') . '/' . $seo2 . '/';
            }

            $seo2 = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
            $cid = isset($row['CatID']) ? $row['CatID'] : NULL;
            $this->_['CatName'] = isset($row['CatName']) ? ucwords($row['CatName']) : NULL;
            $html = $this->view->view_display('index');
            $arr_block_html = $this->view->view_get_block_html($html);

            $current = end($this->_seokey) ? intval(end($this->_seokey)) : 1;
            $total = $this->mdu_sentmail_total_list();

            $url = !empty($cid) ? URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1] . '/' . $seo2 : URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1];


            //$this->_["td_addnew"]= $arr_block_html["add_new"];
            $this->_["td_addnew"] = "";


            /*btn add-edit*/

            //$this->_["btn_tool"] = $arr_block_html["btn_add"] . $arr_block_html["btn_edit"] . $arr_block_html["btn_delete"];
            $this->_["btn_tool"] = $arr_block_html["btn_add"];
            /*end btn add-edit*/


            $rows = $this->mdm_sentmail_list($cid , $current , $limit);
            if (!empty($rows))
                foreach ($rows as $row) {
                    $res_html .= $this->view->view_assign($arr_block_html['product'] , $row);
                }
            $this->_['paging'] = $this->paging_display($total , $current , $limit , $url , $this->_ , ".html");
            $shtml = $this->view->view_assign_block($html , array('htmlproduct' => $res_html));
            $strOkHtml = $this->view->view_assign($shtml , $this->_);
            return $strOkHtml;
        }

        public function index_ajax_action()
        {
            $this->per_check_permission();

            $this->_layout = 'NO_LAYOUT';

            $active = isset($this->_seokey[3]) ? $this->_seokey[3] : null;
            $paremater = isset($this->_seokey[4]) ? $this->_seokey[4] : null;
            $paremater2 = isset($this->_seokey[5]) ? $this->_seokey[5] : null;


            /*sent mail*/

            switch ($active) {
                case "getemail":
                    $rs=$this->mdu_get_email();
                    foreach($rs as $email)
                    {

                        $arr= array($email["Email"],$email["ID"]);
                        $Arr[] = $arr;

                    }
                   return json_encode($Arr);
                    break;
                case "sentmail":
                    $user = $this->mdu_get_user($paremater2);
                    foreach($user as $val)
                    {
                        $strOkHtml = $this->createHtmlMail($user , $this->_["mail_content"]);
                        $this->sentMail($val['ID'],($val['SentMail']+1),$paremater , $strOkHtml , $this->_["mail_subject"]);
                    }

                    break;


            }
        }
        private function  createHtmlMail($user,$content_new)
        {
            $res_html = "";
            $html = $this->view->view_display('mail');
            $arr_block_html = $this->view->view_get_block_html($html);

            $this->_["content_email"] =$content_new;
            if (!empty($user))
                foreach ($user as $row) {
                    $row["Fullname"]= strtolower($row["Fullname"]);
                    $res_html .= $this->view->view_assign($arr_block_html['content'] , $row);
                }
            $shtml = $this->view->view_assign_block($html , array('MAIL' => $res_html));
            $strOkHtml = $this->view->view_assign($shtml , $this->_);
            return $strOkHtml;
        }

        private function  sentMail($ID,$count, $email , $Html , $subject)
        {
            $send_mail = new handle_mail();
            $send_mail->protocol = Emailprotocol;
            $send_mail->parameter = "";
            $send_mail->hostname = Emailhostname;
            $send_mail->username = Emailuser;
            $send_mail->password = Emailpassword;
            $send_mail->port = Emailport;
            $send_mail->mail_set_to(trim($email));
            $send_mail->mail_set_from(Emailuser);
            $send_mail->mail_set_sender(Emailuser);
            $send_mail->mail_set_subject(strip_tags(html_entity_decode($subject , ENT_QUOTES , 'UTF-8')));
            $send_mail->setHtml(html_entity_decode($Html , ENT_QUOTES , 'UTF-8'));
            $send_mail->mail_send();
            $this->mdm_update_sentmail($ID,$count);
        }
        public  function  edit_action()
        {
            $this->per_check_permission();
            $this->_["style"] = "display:none";
            $ID = $this->_seokey[3];
            $email = $this->_seokey[4];
            $this->_layout = 'iframe';
            $html = $this->view->view_display("addMail");
            $arr_block_html = $this->view->view_get_block_html($html);
            $shtml = $this->view->view_assign_block($html , array('htmlCat' => $arr_block_html["mail"]));
            if (isset($_POST["mail_subject"]) && isset($_POST["mail_content"])) {

                if ($_POST["mail_subject"] != "" && $_POST["mail_content"] != "") {
                    if (isset($_POST["mail_save"]) && $_POST["mail_save"]) {
                        $content_new = $this->edit_mail($_POST);
                    } else {
                        $content_new = $_POST["mail_content"];
                    }
                    $user = $this->mdu_get_user($ID);
                    foreach($user as $val)
                    {
                        $html = $this->createHtmlMail($user,$content_new);
                        $this->sentMail($val['ID'],($val['SentMail']+1),$email , $html , $_POST["mail_subject"]);
                    }

                } else {
                    $this->_["style"] = "";
                }
            }

            $this->_["mail_subject_v"] = isset($_POST["mail_subject"]) ? $_POST["mail_subject"] : "";
            $this->_["mail_content_v"] = isset($_POST["mail_content"]) ? $_POST["mail_content"] : "";

            $strOkHtml = $this->view->view_assign($shtml , $this->_);
            return $strOkHtml;
        }
        private function  edit_mail($arr )
        {
            $folder = $this->getDirName(LANG);
            $dirlang = PATH_BASE . "app/admin/view/lang/$folder/sentmail.php";
            include_once($dirlang);
            foreach ($this->_ as $k => $v) {
                if ($k == "mail_content") {
                    $this->_ [$k] = $arr["mail_content"];
                }
                if ($k == "mail_subject") {
                    $this->_ [$k] = $arr["mail_subject"];
                }
            }

            $new = "<?php\r";
            foreach ($this->_ as $k => $v) {
                $new .= "\$_['$k']='$v';\r";
            }
            $fp = fopen($dirlang , "w")or exit("khong tim thay file can mo");
            fwrite($fp , $new);
            fclose($fp);
            return $this->_["mail_content"];
        }


    }
